﻿using System.IO;
using System.Windows;
using System.Windows.Threading;

namespace DrawCurveDemo
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		private DispatcherTimer pushFhrDataTimer;
		private readonly List<int> fhrDatas = new();

		private int timeIndex = 1;
		private int dataIndex = 0;

		public MainWindow()
		{
			InitializeComponent();

			Init();
		}

		private void Init()
		{
			//加载心率模拟数据
			using StreamReader reader = new("fhrdata.txt");
			string line;
			while ((line = reader.ReadLine()) != null)
			{
				fhrDatas.Add(int.Parse(line));
			}

			//定时器模拟后台定时推送心率数据
			pushFhrDataTimer = new DispatcherTimer
			{
				Interval = TimeSpan.FromSeconds(1),
			};
			pushFhrDataTimer.Tick += PushFhrDataTimer_Tick;
			pushFhrDataTimer.Start();
		}

		private void PushFhrDataTimer_Tick(object sender, EventArgs e)
		{
			//一秒钟推送4个数据
			var fhrs = new FhrModel[4];

			for (int i = 0; i < fhrs.Length; i++)
			{
				fhrs[i] = new FhrModel
				{
					Index = timeIndex,
					Fhr1 = fhrDatas[dataIndex],
					Fhr2 = fhrDatas[dataIndex],
					Fhr3 = fhrDatas[dataIndex],
				};

				timeIndex++;
				dataIndex++;
				if (dataIndex >= fhrDatas.Count) dataIndex = 0;
			}

			//推送数据到心率曲线控件
			chart.AddFhrData(fhrs);
		}
	}
}